package br.idez.idezJet.control.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator("emailValidator")
public class EmailValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {

		UIViewRoot viewRoot = context.getViewRoot();
		UIComponent uiComponent = viewRoot.findComponent("form:inputEmail1");

		if (((UIInput) uiComponent).getValue() == null
				|| !((String) ((UIInput) uiComponent).getValue())
						.equals((String) value)) {
			FacesMessage message = new FacesMessage(
					"email.invalido");
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}

	}
}
